<template>
  <div class="department-container">
    <div class="app-container">
      <!--      展示树形结构-->
      <el-tree :data="depts" :props="defaultProps" default-expand-all :expand-on-click-node="false">
        <!--        节点结构-->
        <template v-slot="{data}">
          <el-row style="width: 100%;height: 40px" type="flex" justify="space-between" align="middle">
            <el-col>
              {{ data.name }}
            </el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand($event,data.id)">
                <!--              下拉菜单区域内容-->
                <span class="el-dropdown-link">
                  下拉菜单<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!--              下拉菜单选项-->
                <el-dropdown-menu defalut="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑</el-dropdown-item>
                  <el-dropdown-item command="remove">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <dialog-add-department ref="addDept" :current-node-id="currentNodeId" :visible.sync="dialogFormVisible" :title="title" @update-department="getDepartment" />
  </div>
</template>
<script>
import { delDepartment, getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import DialogAddDepartment from '@/views/department/components/dialog-add-department.vue'

export default {
  name: 'Index',
  components: {
    DialogAddDepartment
  },
  data() {
    return {
      depts: [],
      currentNodeId: null, // 打开的当前节点id
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      dialogFormVisible: false,
      title: ''
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const result = await getDepartment()
      console.log(result, '返回的部门数据')
      this.depts = transListToTreeData(result, 0)
    },
    handleCommand(type, id) {
      if (type === 'add') {
        this.dialogFormVisible = true
        this.currentNodeId = id
        this.title = '新增部门'
      } else if (type === 'edit') {
        this.dialogFormVisible = true
        this.currentNodeId = id
        this.title = '更新部门'
        // 父组件调用自定义组件实例方法通过ref
        // 这里上面的props值的更新是异步的，而下面调用的方法是同步的，会导致执行下面的方法对应的id拿不到，必须等数据更新后，再调用方法
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartment(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartment()
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.app-container{
  padding: 30px 14px;
  font-size: 14px;
  margin: 15px 0 0 15px;
  height: 90vh;
  background: #fff;
//  ::v-deep(.el-col) {
//    height: 40px;
//}
}
.tree-manager{
  margin-right: 20px;
}

</style>
